#include "withdraw_info.h"
#include "QSqlDatabase"
#include "QSqlQuery"
#include "QDebug"
#include "QMessageBox"
#include "QSqlError"
#include "QDateTime"

withdraw_info::withdraw_info()
{

}

void withdraw_info::setCustTelnumber(const QString &telnumber)
{
    cust_telnumber = telnumber;
}

void withdraw_info::setCustName(const QString& name)
{
    cust_name = name;
}
void withdraw_info::setCustPassword(const QString& password)
{
    cust_password = password;
}
void withdraw_info::setCustAddress(const QString& address)
{
    cust_address = address;
}
void withdraw_info::setSavingsType(const QString& type)
{
    savings_type = type;
}
void withdraw_info::setSavingsAmount(const QString& amount)
{
    savings_amount = amount;
}

void withdraw_info::setSavingsTime(const QString &time)
{
    savings_time = time;
}
void withdraw_info::setIsLost(const QString& islost)
{
    is_lost = islost;
}
void withdraw_info::setLostTime(const QString& losttime)
{
    lost_time = losttime;
}
void withdraw_info::setClerkName(const QString& name)
{
    clerk_name = name;
}
void withdraw_info::setClerkEmployeeid(const QString& id)
{
    clerk_employeeid = id;
}
void withdraw_info::setWithdrawTime(const QString &time)
{
    withdraw_time = time;
}

void withdraw_info::setInterest(const QString &interest)
{
    this->interest = interest;
}

void withdraw_info::setWithdrawAmount(const QString &amount)
{
    withdraw_amount = amount;
}

void withdraw_info::setReviewStatus(const QString& status)
{
    review_Status = status;
}

void withdraw_info::insertToDB()
{
    QSqlDatabase sqlite = QSqlDatabase::database("withdraw");
    QSqlQuery query(sqlite);
    //取款窗口已经判断是否可以取款，直接插入取款信息即可
    query.prepare("INSERT INTO WITHDRAW(custtelnumber,custname,custpassword,custaddress,\
                   savingstype,savingsamount,savingstime,islost,losttime,clerkname,clerkemployeeid,\
                   withdrawtime,interest,withdrawamount,reviewstatus)\
                   VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

    query.addBindValue(cust_telnumber);query.addBindValue(cust_name);query.addBindValue(cust_password);
    query.addBindValue(cust_address);query.addBindValue(savings_type);query.addBindValue(savings_amount);query.addBindValue(savings_time);
    query.addBindValue(is_lost);query.addBindValue(lost_time);query.addBindValue(clerk_name);query.addBindValue(clerk_employeeid);
    query.addBindValue(withdraw_time);query.addBindValue(interest);query.addBindValue(withdraw_amount);query.addBindValue(review_Status);
    if(query.exec())
    {
        QMessageBox box;
        box.setWindowTitle("completed");
        box.setText("取款申请成功");
        box.setIcon(QMessageBox::Information);
        box.exec();
    }
    else return;
}

double withdraw_info::calInterest()
{
    QDateTime time1 = QDateTime::fromString(savings_time);
    QDateTime time2 = QDateTime::fromString(withdraw_time);
    qint64 secsDiff = time1.secsTo(time2);
    int daysDiff = secsDiff/86400;
    double money;
    if(savings_type == "1年")
    {
        if(daysDiff < 365)
        {
            money = savings_amount.toInt() * 0.00005 * 0.8 * daysDiff;
        }
        else if(daysDiff > 365)
        {
            money = savings_amount.toInt() *0.0198 * 1 + savings_amount.toInt() * 0.00005 * (daysDiff - 365);
        }
        else
        {
            money = savings_amount.toInt() *0.0198 * 1;
        }
    }
    else if(savings_type == "3年")
    {
        if(daysDiff < 1095)
        {
            money = savings_amount.toInt() * 0.00005 * 0.8 * daysDiff;
        }
        else if(daysDiff > 1095)
        {
            money = savings_amount.toInt() *0.0225 * 3 + savings_amount.toInt() * 0.00005 * (daysDiff - 1095);
        }
        else
        {
            money = savings_amount.toInt() *0.0225 * 3;
        }
    }
    else
    {
        if(daysDiff < 1825)
        {
            money = savings_amount.toInt() * 0.00005 * 0.8 * daysDiff;
        }
        else if(daysDiff > 1825)
        {
            money = savings_amount.toInt() *0.035 * 5 + savings_amount.toInt() * 0.00005 * (daysDiff - 1825);
        }
        else
        {
            money = savings_amount.toInt() *0.035 * 5;
        }
    }
    return money;
}




















